package com.bj.dz.controller;

import com.bj.dz.ResultModel.R;
import com.bj.dz.ResultModel.RHelper;
import com.bj.dz.ResultModel.ResponseCode;
import com.bj.dz.entity.ElectricityMadeHourse;
import com.bj.dz.entity.TrafficBridgeData;
import com.bj.dz.service.ElectricityMadeHourseService;
import com.bj.dz.util.ExcelUtil;
import com.bj.dz.util.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/***
*   发电厂房数据控制器
*/
@Slf4j
@Api(tags = "【发电厂房数据】提供服务接口")
@RestController
@RequestMapping("/electricityMadeHourse")
public class ElectricityMadeHourseController {

    @Resource
    private ElectricityMadeHourseService electricityMadeHourseService;

    /**
     * 查询所有发布任务【根据所关联子任务sid】
     *
     * @param
     * @return
     */
    @ApiOperation(value = "查询水泵数据，根据所关联sid")
    @RequestMapping(value = "/findAllBySid", method = RequestMethod.GET, produces = "application/json;" + "charset=UTF-8")
    public Page findAllByType(@ApiParam(name = "sid", value = "关联的sid", required = true)
                              @RequestParam(value = "sid", required = true) Integer sid,
                              @ApiParam(name = "page", value = "当前页", required = true) @RequestParam(value = "page", required = true) Integer page,
                              @ApiParam(name = "rows", value = "每页显示行数", required = true) @RequestParam(value = "rows", required = true) Integer rows) {
        log.info("关联的子任务id为:" + sid);
        //查询所有发布任务根据模块类型
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("sid", sid);//子任务id
        Page<ElectricityMadeHourse> pageData = electricityMadeHourseService.queryElectricityMadeHoursePageByMap(param, page, rows);//分页相关的一切数据
        return pageData;
    }

    /**
    * 通过id查询
    * @param
    * @return
    */
    @ApiOperation(value = "查找服务【通过发电厂房数据的id查找】【使用】")
    @RequestMapping(value="/findById",method = RequestMethod.POST, produces = "application/json;" +"charset=UTF-8")
    public R findElectricityMadeHourseById(@ApiParam(name = "id",value = "发电厂房数据的id",required = true) @RequestParam(value = "id",required=true)Integer id){
        R r= RHelper.ok();
        r.setDatas(electricityMadeHourseService.getElectricityMadeHourseById(id));
        return r;
    }

    /**
     * 查找多个服务【通过子任务id查询发电厂房数据】
     * @param
     * @return
     */
    @ApiOperation(value = "查找多个服务【通过子任务id查询发电厂房数据】【使用】")
    @RequestMapping(value="/findsBySid",method = RequestMethod.POST, produces = "application/json;" +"charset=UTF-8")
    public R findsBySid(@ApiParam(name = "sid",value = "子任务id",required = true)@RequestParam(value = "sid",required=true) Integer sid){
        R r= RHelper.ok();
        Map<String,Object> param=new HashMap<String,Object>();
        param.put("sid",sid);
        List<ElectricityMadeHourse> electricityMadeHourseList=electricityMadeHourseService.getElectricityMadeHourseListByMap(param);
        r.setDatas(electricityMadeHourseList);
        return r;
    }

    /**
    * 通过多个查询
    * @param
    * @return
    */
    @ApiOperation(value = "查找多个服务")
    @RequestMapping(value="/findElectricityMadeHourses",method = RequestMethod.POST, produces = "application/json;" +"charset=UTF-8")
    public List<ElectricityMadeHourse> findElectricityMadeHourseListByMap(@ApiParam(name = "ElectricityMadeHourse",value = "electricityMadeHourse",required = true)@RequestBody ElectricityMadeHourse electricityMadeHourse){

        return electricityMadeHourseService.getElectricityMadeHourseListByMap(null);
    }

    /**
    * 查询总数
    * @param
    * @return
    */
    @ApiOperation(value = "获取总数服务")
    @RequestMapping(value="/count",method = RequestMethod.POST, produces = "application/json;" +"charset=UTF-8")
    public Integer ElectricityMadeHourseCount(@ApiParam(name = "sid", value = "对应子任务的sid", required = true) @RequestParam(value = "sid", required = true) Integer sid) {
        log.info("对应子任务的sid为:" + sid);
        //查询所有发布任务根据模块类型
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("sid", sid);//模块类型
        return electricityMadeHourseService.getElectricityMadeHourseCountByMap(param);
    }

    /**
    * 添加保存【传入子任务id和发电厂房数据的数据】
    * @param
    * @return
    */
    @ApiOperation(value = "添加服务【使用】")
    @RequestMapping(value="/add",method = RequestMethod.POST, produces = "application/json;" +"charset=UTF-8")
    public R addElectricityMadeHourse(@ApiParam(name = "ElectricityMadeHourse",value = "发电厂房数据【对应子任务id必填】",required = false) ElectricityMadeHourse electricityMadeHourse){
        R r= RHelper.ok();
        //获取子任务id
        Integer sid=electricityMadeHourse.getSid();
        if(sid==null || sid==0){
            r.setCode(ResponseCode.FAILED_CODE);
            r.setMsg("添加失败，未指定子任务");
            return r;
        }
        boolean flag=electricityMadeHourseService.addElectricityMadeHourse(electricityMadeHourse);
        if(flag){
            r.setMsg("添加发电厂房数据成功");
        }else{
            r.setCode(ResponseCode.FAILED_CODE);
            r.setMsg("添加发电厂房数据失败");
        }
        return r;
    }

    /**
    * 更新修改【传入子任务id和水泵的数据(还有id)】
    * @param
    * @return
    */
    @ApiOperation(value = "更新修改服务【使用】")
    @RequestMapping(value="/upd",method = RequestMethod.POST, produces = "application/json;" +"charset=UTF-8")
    public R modifyElectricityMadeHourse(@ApiParam(name = "ElectricityMadeHourse",value = "发电厂房数据和对应子任务id【发电厂房数据id都必填】",required = false) ElectricityMadeHourse electricityMadeHourse){
        R r= RHelper.ok();
        //获取子任务id
        Integer sid=electricityMadeHourse.getSid();
        if(sid==null || sid==0){
            r.setCode(ResponseCode.FAILED_CODE);
            r.setMsg("更新失败，未指定子任务");
            return r;
        }
        //获取id
        Integer id=electricityMadeHourse.getId();
        if(id==null || id==0){
            r.setCode(ResponseCode.FAILED_CODE);
            r.setMsg("更新失败，未找到更新的发电厂房数据");
            return r;
        }
        boolean flag=electricityMadeHourseService.modifyElectricityMadeHourse(electricityMadeHourse);
        if(flag){
            r.setMsg("更新发电厂房数据成功");
        }else{
            r.setCode(ResponseCode.FAILED_CODE);
            r.setMsg("更新发电厂房数据失败");
        }
        return r;
    }

    /**
     * 子任务exce解析
     *
     * @param file
     * @param sid
     * @return
     */
    @ApiOperation(value = "子任务excel解析")
    @RequestMapping(value = "/upExcel", method = RequestMethod.POST, produces = "application/json;" + "charset=UTF-8")
    public boolean addReleaseSubtast(@ApiParam(name = "file", value = "上传的Excel文件", required = true) @RequestParam(value = "file", required = true) MultipartFile file,
                                     @ApiParam(name = "sid", value = "水泵对应的子任务sid", required = true) @RequestParam(value = "sid", required = true) Integer sid) {
        List<ElectricityMadeHourse> electricityMadeHourses = null;
        boolean flag = false;
        try {
            electricityMadeHourses = ExcelUtil.importExcel(file.getInputStream(), "yyyy-MM-dd hh:mm:ss", 0, 1, 0, ElectricityMadeHourse.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
//        assert releaseSubtasks != null;
        for (ElectricityMadeHourse e : electricityMadeHourses) {
            e.setSid(sid);
            flag = electricityMadeHourseService.addElectricityMadeHourse(e);
            if (!flag) {
                return false;
            }
        }
        return flag;
    }

    /**
    * 删除
    * @param
    * @return
    */
    @ApiOperation(value = "删除服务")
    @RequestMapping(value="/del",method = RequestMethod.POST, produces = "application/json;" +"charset=UTF-8")
    public boolean deleteElectricityMadeHourseById(@ApiParam(name = "ElectricityMadeHourse",value = "electricityMadeHourse的id",required = true) @RequestParam(value = "id",required=true)Integer id){
        return electricityMadeHourseService.deleteElectricityMadeHourseById(id);
    }



}
